package org.erp.spu.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.log4j.Log4j2;
import org.erp.common.page.PageQueryModel;

import org.erp.spu.entity.SpuCategory;
import org.erp.spu.mapper.SpuCategoryMapper;

import org.erp.spu.entity.bo.SpuCategoryBo;

import org.erp.spu.entity.dto.SpuCategoryDto;
import org.erp.spu.entity.vo.SpuCategoryVo;
import org.erp.spu.service.SpuCategoryService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 分类表(SpuCategory)表服务实现类
 *
 * @author makejava
 * @since 2025-11-05 10:10:45
 */
@Log4j2
@Service("spuCategoryService")
public class SpuCategoryServiceImpl extends ServiceImpl<SpuCategoryMapper, SpuCategory> implements SpuCategoryService {

    @Resource
    private SpuCategoryMapper spuCategoryMapper;
    @Override
    public List<SpuCategoryVo> getList(SpuCategoryBo spuCategoryBo) {
        LambdaQueryWrapper<SpuCategory> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(StringUtils.hasText(spuCategoryBo.getCategoryName()), SpuCategory::getCategoryName, spuCategoryBo.getCategoryName());
        queryWrapper.eq(SpuCategory::getDelFlag, 0);

        List<SpuCategory> categories = spuCategoryMapper.selectList(queryWrapper);
        return categories.stream()
                .map(this::convertToVo)
                .collect(Collectors.toList());
    }

        /**
         * 分页查询所有数据
        * @param spuCategoryBo
        * @return
        */
        @Override
        public PageQueryModel<SpuCategoryVo> getListByPage(SpuCategoryBo spuCategoryBo) {
            LambdaQueryWrapper<SpuCategory> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.like(StringUtils.hasText(spuCategoryBo.getCategoryName()), SpuCategory::getCategoryName, spuCategoryBo.getCategoryName());
            queryWrapper.eq(StringUtils.hasText(spuCategoryBo.getStatus()), SpuCategory::getStatus, spuCategoryBo.getStatus());
            queryWrapper.eq(SpuCategory::getDelFlag, 0);
            Page<SpuCategory> page = spuCategoryMapper.selectPage(new Page<SpuCategory>(spuCategoryBo.getPageNum(), spuCategoryBo.getPageSize()), queryWrapper);
            log.info("分页查询结果：{}", page.getRecords());
            List<SpuCategoryVo> voList = page.getRecords().stream()
                    .map(this::convertToVo)
                    .collect(Collectors.toList());
            return PageQueryModel.of(page.getPages(), voList);
        }

    /**
     * 新增数据
     *
     * @param spuCategoryDto
     * @return
     */
    @Override
    public boolean addSpuCategory(SpuCategoryDto spuCategoryDto) {
            Assert.isTrue(!checkCategoryName(spuCategoryDto), "分类名称已存在");
            SpuCategory spuCategory = new SpuCategory();
            BeanUtils.copyProperties(spuCategoryDto, spuCategory);
        return spuCategoryMapper.insert(spuCategory) > 0;
    }
    /**
     * 修改数据
     *
     * @param spuCategoryDto
     * @return
     */
    @Override
    public boolean updateSpuCategory(SpuCategoryDto spuCategoryDto) {
        Assert.isTrue(!checkCategoryName(spuCategoryDto), "分类名称已存在");
        SpuCategory spuCategory = new SpuCategory();
        BeanUtils.copyProperties(spuCategoryDto, spuCategory);
        return spuCategoryMapper.updateById(spuCategory) > 0;
    }

    @Override
    public SpuCategoryVo getSpuCategoryVoById(Long id) {
        SpuCategory category = spuCategoryMapper.selectById(id);
        return convertToVo( category);
    }

    /**
     * 转换为VO对象
     *
     * @param category
     * @return
     */
    private SpuCategoryVo convertToVo(SpuCategory category) {
        SpuCategoryVo vo = new SpuCategoryVo();
        // 进行属性拷贝
        BeanUtils.copyProperties(category, vo);
        return vo;
    }
    /**
     * 检查分类名称是否存在
     *
     * @param spuCategoryDto
     * @return
     */
    private boolean checkCategoryName(SpuCategoryDto spuCategoryDto) {
        LambdaQueryWrapper<SpuCategory> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SpuCategory::getCategoryName, spuCategoryDto.getCategoryName());
        queryWrapper.eq(SpuCategory::getDelFlag, 0);
        // 如果是修改操作，排除当前记录
        if (spuCategoryDto.getId() != null) {
            queryWrapper.ne(SpuCategory::getId, spuCategoryDto.getId());
        }
        return this.count(queryWrapper) > 0;
    }



}

